// ServletInputStream.java - Implements javax.servlet.ServletInputStream.
//
// Copyright (C) 1999-2002  Smart Software Consulting
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// Smart Software Consulting
// 1688 Silverwood Court
// Danville, CA  94526-3079
// USA
//
// http://www.smartsc.com
//

package com.smartsc.http;

import java.io.BufferedInputStream;
import java.io.IOException;

public class
ServletInputStream
extends javax.servlet.ServletInputStream
{
	protected
	ServletInputStream( BufferedInputStream is)
	{
		this.is = is;
	}

	// From java.io.InputStream
	public int available() throws IOException { return is.available();}
	public void close() throws IOException { is.close(); }
	public synchronized void mark(int readLimit) { is.mark( readLimit); }
	public boolean markSupported() { return is.markSupported(); }
	public int read() throws IOException { return is.read(); }
	public int read(byte[] b) throws IOException { return is.read( b); }
	public int read(byte[] b, int off, int len) throws IOException
	{
		return is.read( b, off, len);
	}
	public synchronized void reset() throws IOException { is.reset(); }
	public long skip( long n)
	throws IOException { return is.skip( n); }

	// From javax.servlet.ServletInputStream
	public int readLine( byte[] buf, int off, int len)
	throws IOException
	{
		int b = 0;
		int i = 0;
		for( i = 0; (i < len) && (b != '\n'); ++i)
		{
			b = is.read();
			// Treat CRLF as LF
			if( b == '\r')
			{
				// Save position in stream
				is.mark( 1);
				// Read next character
				b = is.read();
				// If not newline
				if( b != '\n')
				{
					// Put it back
					is.reset();
					// Restore b to CR
					b = '\r';
				}
			}
			buf[off+i] = (byte)b;
		}

		return i;
	}

	private BufferedInputStream is;
}
